package com.inossem.warehousestorage_inossem.request.inventory;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.inossem.warehousestorage_inossem.activity.inventory.InventoryExtra;
import com.inossem.warehousestorage_inossem.bean.inventory.InventoryAreaBean;
import com.inossem.warehousestorage_inossem.bean.inventory.InventoryBatchBean;
import com.inossem.warehousestorage_inossem.bean.inventory.InventoryMaterielListBean;
import com.inossem.warehousestorage_inossem.bean.inventory.InventoryMgrBean;
import com.inossem.warehousestorage_inossem.bean.inventory.InventoryNewMatQueryReqBean;
import com.inossem.warehousestorage_inossem.bean.inventory.InventoryNewMatQueryRespBean;
import com.inossem.warehousestorage_inossem.bean.inventory.InventoryNewMatReqBean;
import com.inossem.warehousestorage_inossem.bean.inventory.InventoryOrdinaryMatReqBean;
import com.inossem.warehousestorage_inossem.bean.inventory.InventoryOrdinaryMaterieBean;
import com.inossem.warehousestorage_inossem.bean.inventory.InventoryPositionListBean;
import com.inossem.warehousestorage_inossem.bean.inventory.InventoryRespBean;
import com.inossem.warehousestorage_inossem.bean.inventory.InventorySpeInvCodeListBean;
import com.inossem.warehousestorage_inossem.bean.inventory.InventorySpeInvFlagListBean;
import com.inossem.warehousestorage_inossem.bean.inventory.InventorySupplierListBean;
import com.inossem.warehousestorage_inossem.constant.RequestConstant;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.listener.StringRequestListener;
import com.inossem.warehousestorage_inossem.manage.RequestManage;
import com.inossem.warehousestorage_inossem.model.StringResponse;
import com.inossem.warehousestorage_inossem.request.Request;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by leij on 2017/6/12.
 */

public class InventoryManageRequest extends Request {
    private static final String CONDITION = "condition";//查询条件
    private static final String SPECIAL_INVENTORY_FLAG = "specialInventoryFlag";
    private static final String ID = "id";
    private static final String FACTORY_CODE = "factoryCode";
    private static final String MATERIEL_CODE = "materielCode";
    private static final String INVENTORY_VOUCHER = "inventoryVoucher";

    public static String request(Context context, GsonBeanListener gsonBeanListener) {
        String tag;
        String url = RequestManage.getUrl(context,RequestConstant.URL_INVENTORY_LIST);
        tag = RequestManage.getHttpsGetWithLoading(context, url, new StringRequestListener() {
            @Override
            public void onSuccess(StringResponse stringRequestResponse) {
                Gson gson = new Gson();
                String json = stringRequestResponse.getResponseInfo();
                InventoryMgrBean bean = gson.fromJson(json, InventoryMgrBean.class);
                excuteJsonBean(context, bean, bean.getCode(), bean.getMsg(), gsonBeanListener);
            }
        });
        return tag;
    }


    public static String overview(Context context, String inventoryVoucherStr, GsonBeanListener gsonBeanListener) {
        String tag;
        Map<String, String> params = new HashMap<>();
        params.put(INVENTORY_VOUCHER, inventoryVoucherStr);
        String url = RequestManage.getUrl(context,RequestConstant.URL_INVENTORY_AREA_LIST)+RequestManage.mapEncodeToParams(params);
       tag = RequestManage.getHttpsGetWithLoading(context, url, new StringRequestListener() {
            @Override
            public void onSuccess(StringResponse stringRequestResponse) {
                Gson gson = new Gson();
                String json = stringRequestResponse.getResponseInfo();
                InventoryAreaBean bean = gson.fromJson(json, InventoryAreaBean.class);
                excuteJsonBean(context, bean, bean.getCode(), bean.getMsg(), gsonBeanListener);
            }
        });
        return tag;
    }



    public static String commit(Context context, String inventoryVoucherStr, GsonBeanListener gsonBeanListener) {
        String tag;
        Gson gson = new Gson();
        Map<String, String> params = new HashMap<>();
         params.put(INVENTORY_VOUCHER, inventoryVoucherStr);
        String url = RequestManage.getUrl(context,RequestConstant.URL_INVENTORY_COMMIT)+ RequestManage.mapEncodeToParams(params);
        tag = RequestManage.getHttpsGetWithLoading(context, url, new StringRequestListener() {
            @Override
            public void onSuccess(StringResponse stringRequestResponse) {

                String json = stringRequestResponse.getResponseInfo();
                InventoryRespBean bean = gson.fromJson(json, InventoryRespBean.class);
                excuteJsonBean(context, bean, bean.getCode(), bean.getMsg(), gsonBeanListener);
            }
        });
        return tag;
    }


    /**
     * 查找普通物料
     * @param context
     * @param gsonBeanListener
     * @return
     */
    public static String findOrdList(Context context, InventoryNewMatQueryReqBean queryReqBean, GsonBeanListener gsonBeanListener) {
        String tag;
        String url = RequestManage.getUrl(context,RequestConstant.URL_INVENTORY_ITEM_LIST);
        Gson gson = new Gson();
        String body = gson.toJson(queryReqBean);
        tag = RequestManage.getHttpsPostWithLoading(context, url, body,new StringRequestListener() {
            @Override
            public void onSuccess(StringResponse stringRequestResponse) {
                Gson gson = new Gson();
                String json = stringRequestResponse.getResponseInfo();
                InventoryOrdinaryMaterieBean bean = gson.fromJson(json, InventoryOrdinaryMaterieBean.class);
                excuteJsonBean(context, bean, bean.getCode(), bean.getMsg(), gsonBeanListener);
            }
        });
        return tag;
    }

    public static String updateItems(Context context, InventoryOrdinaryMatReqBean bean, GsonBeanListener gsonBeanListener) {
        String tag;
        String url = RequestManage.getUrl(context,RequestConstant.URL_INVENTORY_UPDATEITEMS);
        Gson gson = new Gson();
        String body = gson.toJson(bean);
        tag = RequestManage.getHttpsPostWithLoading(context, url,body, new StringRequestListener() {
            @Override
            public void onSuccess(StringResponse stringRequestResponse) {
                Gson gson = new Gson();
                String json = stringRequestResponse.getResponseInfo();
                InventoryRespBean bean = gson.fromJson(json, InventoryRespBean.class);
                excuteJsonBean(context, bean, bean.getCode(), bean.getMsg(), gsonBeanListener);
            }
        });
        return tag;
    }


    public static String saveNewItem(Context context, InventoryNewMatReqBean bean, GsonBeanListener gsonBeanListener) {
        String tag;
        String url = RequestManage.getUrl(context,RequestConstant.URL_INVENTORY_SAVE_NEW_ITEM);
        Gson gson = new Gson();
        String body = gson.toJson(bean);
        tag = RequestManage.getHttpsPostWithLoading(context, url,body, new StringRequestListener() {
            @Override
            public void onSuccess(StringResponse stringRequestResponse) {
                Gson gson = new Gson();
                String json = stringRequestResponse.getResponseInfo();
                InventoryRespBean bean = gson.fromJson(json, InventoryRespBean.class);
                excuteJsonBean(context, bean, bean.getCode(), bean.getMsg(), gsonBeanListener);
            }
        });
        return tag;
    }


    public static String findMateriel(Context context, String condition, String factoryCode, String inventoryAddressCode, GsonBeanListener gsonBeanListener) {
        String tag;
        Map<String, String> params = new HashMap<>();
        if(!TextUtils.isEmpty(factoryCode)){
            params.put(InventoryExtra.FACTORY_CODE,factoryCode);
        }
        if(!TextUtils.isEmpty(inventoryAddressCode)){
            params.put(InventoryExtra.INVENTORY_ADDRESS_CODE, inventoryAddressCode);
        }
        if(!TextUtils.isEmpty(condition)){
            params.put(CONDITION, condition);
        }
        String url = RequestManage.getUrl(context,RequestConstant.URL_INVENTORY_SELECT_MATERIEL + RequestManage.mapEncodeToParams(params));
        tag = RequestManage.getHttpsGetWithLoading(context, url, new StringRequestListener() {
            @Override
            public void onSuccess(StringResponse stringRequestResponse) {
                Gson gson = new Gson();
                String responseInfo = stringRequestResponse.getResponseInfo();
                InventoryMaterielListBean bean = gson.fromJson(responseInfo, InventoryMaterielListBean.class);
                excuteJsonBean(context, bean, bean.getCode(), bean.getMsg(), gsonBeanListener);
            }
        });
        return tag;
    }


    public static String findPosition(Context context, String condition, String factoryCode, String inventoryAddressCode, GsonBeanListener gsonBeanListener) {
        String tag;
        Map<String, String> params = new HashMap<>();
        if(!TextUtils.isEmpty(factoryCode)){
            params.put(InventoryExtra.FACTORY_CODE, factoryCode);
        }
        if(!TextUtils.isEmpty(inventoryAddressCode)){
            params.put(InventoryExtra.INVENTORY_ADDRESS_CODE, inventoryAddressCode);
        }
        if(!TextUtils.isEmpty(condition)){
            params.put(CONDITION, condition);
        }
        String url = RequestManage.getUrl(context,RequestConstant.URL_INVENTORY_POSITION_LIST + RequestManage.mapEncodeToParams(params));
        tag = RequestManage.getHttpsGetWithLoading(context, url, new StringRequestListener() {
            @Override
            public void onSuccess(StringResponse stringRequestResponse) {
                Gson gson = new Gson();
                String responseInfo = stringRequestResponse.getResponseInfo();
                InventoryPositionListBean bean = gson.fromJson(responseInfo, InventoryPositionListBean.class);
                excuteJsonBean(context, bean, bean.getCode(), bean.getMsg(), gsonBeanListener);
            }
        });
        return tag;
    }

    public static String findSpeInvCode(Context context, String condition, String specialInventoryFlag, GsonBeanListener gsonBeanListener) {
        String tag;
        Map<String, String> params = new HashMap<>();
        if(!TextUtils.isEmpty(specialInventoryFlag)){
            params.put(SPECIAL_INVENTORY_FLAG, specialInventoryFlag);
        }
        if(!TextUtils.isEmpty(condition)){
            params.put(CONDITION, condition);
        }
        String url = RequestManage.getUrl(context,RequestConstant.URL_INVENTORY_SPE_INV_CODE_LIST + RequestManage.mapEncodeToParams(params));
        tag = RequestManage.getHttpsGetWithLoading(context, url, new StringRequestListener() {
            @Override
            public void onSuccess(StringResponse stringRequestResponse) {
                Gson gson = new Gson();
                String responseInfo = stringRequestResponse.getResponseInfo();
                InventorySpeInvCodeListBean bean = gson.fromJson(responseInfo, InventorySpeInvCodeListBean.class);
                excuteJsonBean(context, bean, bean.getCode(), bean.getMsg(), gsonBeanListener);
            }
        });
        return tag;
    }

    public static String findSupplier(Context context, String condition, GsonBeanListener gsonBeanListener) {
        String tag;
        Map<String, String> params = new HashMap<>();
        if(!TextUtils.isEmpty(condition)){
            params.put(CONDITION, condition);
        }
        String url = RequestManage.getUrl(context,RequestConstant.URL_INVENTORY_SUPP_LIST + RequestManage.mapEncodeToParams(params));
        tag = RequestManage.getHttpsGetWithLoading(context, url, new StringRequestListener() {
            @Override
            public void onSuccess(StringResponse stringRequestResponse) {
                Gson gson = new Gson();
                String responseInfo = stringRequestResponse.getResponseInfo();
                InventorySupplierListBean bean = gson.fromJson(responseInfo, InventorySupplierListBean.class);
                excuteJsonBean(context, bean, bean.getCode(), bean.getMsg(), gsonBeanListener);
            }
        });
        return tag;
    }


    public static String findSpeInvFlag(Context context, GsonBeanListener gsonBeanListener) {
        String tag;
        String url = RequestManage.getUrl(context,RequestConstant.URL_INVENTORY_SPE_INV_FLAG_LIST );
        tag = RequestManage.getHttpsGetWithLoading(context, url, new StringRequestListener() {
            @Override
            public void onSuccess(StringResponse stringRequestResponse) {
                Gson gson = new Gson();
                String responseInfo = stringRequestResponse.getResponseInfo();
                InventorySpeInvFlagListBean bean = gson.fromJson(responseInfo, InventorySpeInvFlagListBean.class);
                excuteJsonBean(context, bean, bean.getCode(), bean.getMsg(), gsonBeanListener);
            }
        });
        return tag;
    }

    public static String getNewMateriel(Context context, InventoryNewMatQueryReqBean bean, GsonBeanListener gsonBeanListener) {
        String tag;
        Gson gson = new Gson();
        String url = RequestManage.getUrl(context,RequestConstant.URL_INVENTORY_NEW_ITEM_LIST);
        String body = gson.toJson(bean);
        tag= RequestManage.getHttpsPostWithLoading(context, url,body, new StringRequestListener() {
            @Override
            public void onSuccess(StringResponse stringRequestResponse) {

                String json = stringRequestResponse.getResponseInfo();
                InventoryNewMatQueryRespBean bean = gson.fromJson(json, InventoryNewMatQueryRespBean.class);
                excuteJsonBean(context, bean, bean.getCode(), bean.getMsg(), gsonBeanListener);
            }
        });
        return tag;
    }


    public static String delNewMateriel(Context context, String id,String factoryCode, GsonBeanListener gsonBeanListener) {
        String tag;
        Map<String, String> params = new HashMap<>();
        if(!TextUtils.isEmpty(factoryCode)){
            params.put(FACTORY_CODE, factoryCode);
        }
        if(!TextUtils.isEmpty(id)){
            params.put(ID, id);
        }
        String url = RequestManage.getUrl(context,RequestConstant.URL_INVENTORY_DEL_NEW_ITEM)+ RequestManage.mapEncodeToParams(params);
        tag = RequestManage.getHttpsGetWithLoading(context, url, new StringRequestListener() {
            @Override
            public void onSuccess(StringResponse stringRequestResponse) {
                Gson gson = new Gson();
                String json = stringRequestResponse.getResponseInfo();
                InventoryRespBean bean = gson.fromJson(json, InventoryRespBean.class);
                excuteJsonBean(context, bean, bean.getCode(), bean.getMsg(), gsonBeanListener);
            }
        });
        return tag;
    }

    public static String findBatch(Activity context, String materielCode, String factoryCode, GsonBeanListener<InventoryBatchBean> gsonBeanListener) {

        String tag;
        Map<String, String> params = new HashMap<>();
        if(!TextUtils.isEmpty(factoryCode)){
            params.put(FACTORY_CODE, factoryCode);
        }
        if(!TextUtils.isEmpty(materielCode)){
            params.put(MATERIEL_CODE, materielCode);
        }
        String url = RequestManage.getUrl(context,RequestConstant.URL_INVENTORY_BATCH_LIST)+ RequestManage.mapEncodeToParams(params);
        tag = RequestManage.getHttpsGetWithLoading(context, url, new StringRequestListener() {
            @Override
            public void onSuccess(StringResponse stringRequestResponse) {
                Gson gson = new Gson();
                String json = stringRequestResponse.getResponseInfo();

                InventoryBatchBean bean = gson.fromJson(json, InventoryBatchBean.class);
                excuteJsonBean(context, bean, bean.getCode(), bean.getMsg(), gsonBeanListener);
            }
        });
        return tag;
    }
}
